// pages/recruit/personal/authentication/addman/addman.js
const schools = require("../../../../../utils/AllSchool.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    range: ["居民身份证", "临时居民身份证", "护照", "港澳居民来往内地通行证", "台湾居民来往大陆通行证"],
    height: "",
    boxheight: "",
    persontype: 1,
    zimageurl: "",
    fimageurl: "",
    scimageurl: "",
    name: "",
    idtype: "",
    id: "",
    schoolname: "点击选择",
    graduatetime: "点击选择",
    show: "noshow",
    inputvalue: [],
    isChecked: true,
    nocommit: "",
    idindex: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    getApp().globalData.get("/api/resume/personal/certification", {}).then(res => {
      console.log(res.data)
      if (res.data != null) {
        that.setData({
          idindex: res.data.id,
          zimageurl: res.data.idcardFront,
          fimageurl: res.data.idcardReverse,
          scimageurl: res.data.handheld,
          name: res.data.name,
          id: res.data.code,
          schoolname: res.data.school,
          graduatetime: res.data.graduationDate.join("-")
        })
      }
    })
    //获取七牛云的token
    getApp().globalData.get("/api/qiniu/auth", {}).then(res => {
      if (res.data != null) {
        that.data.qiniuToken = res.data
      }
    })
    that.setData({
      persontype: options.type | 1
    })
    var query = wx.createSelectorQuery();
    query.select('.getidimg').boundingClientRect(function(rect) {
      //身份证的宽高比为85.6/54
      that.setData({
        height: rect.width * 54 / 85.6 * 2
      })
    }).exec();
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          boxheight: res.windowHeight
        })
      }
    });
  },
  uploadimg: function(e) { //上传身份证图片
    const that = this;
    let index = e.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        wx.uploadFile({
          url: 'https://up-z2.qiniup.com',
          name: 'file',
          filePath: res.tempFilePaths[0],
          formData: {
            token: that.data.qiniuToken,
          },
          success: function(res) {
            console.log(res)
            let imgurl = "https://image.xuegongbang.cn/" + JSON.parse(res.data).key;
            if (index == 1) {
              that.data.zimageurl = imgurl;
            } else if (index == 2) {
              that.data.fimageurl = imgurl;
            } else {
              that.data.scimageurl = imgurl;
            }
            that.setData({
              zimageurl: that.data.zimageurl,
              fimageurl: that.data.fimageurl,
              scimageurl: that.data.scimageurl
            })
          },
          fail: function(res) {
            wx.showToast({
              title: '服务错误',
              icon: "warn",
            })
          }
        })

      }
    })
  },
  getname: function(e) { //获取姓名
    const that = this;
    let reg = /^[\u4e00-\u9fa5]{0,}$/;
    if (reg.test(e.detail.value)) {
      that.setData({
        name: e.detail.value,
      })
    } else {
      wx.showToast({
        title: '请输入汉字',
        icon: "none",
        duration: 1000
      })
    }
  },
  getid: function(e) { //获取身份证号。台胞证、港澳证以后再说。
    const that = this;
    let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    if (e.detail.value.length == 18) {
      if (reg.test(e.detail.value)) {
        that.setData({
          id: e.detail.value,
        })
      } else {
        wx.showToast({
          title: "身份证号码有误，请重新输入！",
          icon: "none",
          duration: 1000
        })
      }
    }
  },
  seexy: function() { //查看用户协议
    wx.navigateTo({
      url: '../xieyi/xieyi',
    })
  },
  getidtype: function(e) { //获取身份证种类
    console.log(e)
    console.log(this.data.range[e.detail.value])
  },
  selectschool: function(e) { //显示选择学校的页面
    this.setData({
      show: ""
    })
  },
  searchschool: function(e) { //搜索学校信息
    const that = this;
    let val = e.detail.value;
    let newarr = [];
    for (let sc of schools) {
      if (sc.indexOf(val) != -1 && val.length != 0) {
        newarr.push(sc)
        that.setData({
          inputvalue: newarr
        })
      }
    }
    newarr = [];
  },
  graduatetime: function(e) { //获取毕业年份
    this.setData({
      graduatetime: e.detail.value
    })
  },
  clickschool: function(e) { //点击选择学校
    let schoolname = e.currentTarget.dataset.value;
    this.setData({
      schoolname: schoolname,
      show: "noshow"
    })
  },
  cancle: function() { //选择学校页返回
    this.setData({
      show: "noshow"
    })
  },
  commit: function(e) { //提交身份认证信息
    const that = this;
    let type = e.currentTarget.dataset.commit;
    let title;
    if (type == "nocommit") {
      title = "请仔细阅读协议后并勾选同意"
    } else {
      if (that.data.zimageurl == "") {
        title = "请上传身份证人像面"
      } else if (that.data.fimageurl == "") {
        title = "请上传身身份证国徽面"
      } else if (that.data.scimageurl == "") {
        title = "请上传手持身份证"
      } else if (that.data.name == "") {
        title = "请输入你的姓名"
      } else if (that.data.id == "") {
        title = "请输入你的身份证号"
      } else if (that.data.schoolname == "点击选择" && that.data.persontype == 1) {
        title = "请选择你的学校"
      } else if (that.data.graduatetime == "点击选择" && that.data.persontype == 1) {
        title = "请选择毕业年份"
      } else {
        if (that.data.idindex == "") {
          getApp().globalData.post("/api/resume/personal/certification", {
            IDCardFront: that.data.zimageurl,
            IDCardReverse: that.data.fimageurl,
            handheld: that.data.scimageurl,
            name: that.data.name,
            type: 1,
            code: that.data.id,
            school: that.data.schoolname,
            graduationDate: that.data.graduatetime,
          }).then(res => {
            if (res.code == 200){
              wx.showToast({
                title: "提交成功",
                icon: "none",
                duration: 1000
              })
            }
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/recruit/personal/personal',
              })
            }, 2000)
          })
        } else {
          getApp().globalData.post("/api/resume/personal/certification", {
            id: that.data.idindex,
            IDCardFront: that.data.zimageurl,
            IDCardReverse: that.data.fimageurl,
            handheld: that.data.scimageurl,
            name: that.data.name,
            type: 1,
            code: that.data.id,
            school: that.data.schoolname,
            graduationDate: that.data.graduatetime,
          }).then(res => {
            if (res.code == 200) {
              wx.showToast({
                title: "修改",
                icon: "none",
                duration: 1000
              })
            }
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/recruit/personal/personal',
              })
            }, 2000)
          })
        }
        return;
      }
    }
    wx.showToast({
      title: title,
      icon: "none",
      duration: 1500
    })
  },
  checkxy: function(e) {
    let isChecked = e.currentTarget.dataset.ischecked;
    if (isChecked) {
      this.setData({
        isChecked: false,
        nocommit: "nocommit"
      })
    } else {
      this.setData({
        isChecked: true,
        nocommit: ""
      })
    }
  }
})